package com.bookstore.Controller;

import com.bookstore.Domain.Code;
import com.bookstore.Domain.Shopping;
import com.bookstore.Domain.Result;
import com.bookstore.Service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 未来
 */
@CrossOrigin
@Controller
@RequestMapping("/orders")
public class ShoppingController {

    private final ShoppingService shoppingService;

    @Autowired
    public ShoppingController(ShoppingService shoppingService) {
        this.shoppingService = shoppingService;
    }


    @GetMapping
    @ResponseBody
    public Result getAll() {
        List<Shopping> orders = shoppingService.findAll();
        Integer code = orders != null ? Code.GET_OK : Code.GET_ERR;
        String msg = orders != null ? Code.MSG_OK: Code.MSG_ERR;
        return new Result(code, orders, msg);
    }

    /**
     * 根据id删除订单
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ResponseBody
    public Result delete(@PathVariable Integer id) {
        boolean flag = shoppingService.deleteShoppping(id);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    /**
     * 添加订单（普通用户）
     * @param shopping
     * @return
     */
    @PostMapping
    @ResponseBody
    public  Result insert(@RequestBody Shopping shopping){
        boolean flag = shoppingService.insertShoppping(shopping);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    /**
     * 更新订单数据
     * @param shopping
     * @return
     */
    @PutMapping
    @ResponseBody
    public Result update(@RequestBody Shopping shopping){
        boolean flag = shoppingService.update(shopping);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    /**
     * 添加订单（管理员）
     * @param shopping
     * @return
     */
    @PostMapping("/user")
    @ResponseBody
    public  Result insertDate(@RequestBody Shopping shopping){
        boolean flag = shoppingService.insertShoppping(shopping);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    @GetMapping("/shops/{userNumber}")
    @ResponseBody
    public Result getAShops(@PathVariable String userNumber) {
        System.out.println("userNumber"+userNumber);
        List<Shopping> orders = shoppingService.getAllShops(userNumber);
        Integer code = orders != null ? Code.GET_OK : Code.GET_ERR;
        String msg = orders != null ? Code.MSG_OK: Code.MSG_ERR;
        return new Result(code, orders, msg);
    }

    /**
     * 删除包含的id值的数组一个一个删除
     * @param data
     * @return
     */
    @DeleteMapping
    @ResponseBody
    public Result deleteShoppings(@RequestBody int[] data) {
        for (int i = 0; i < data.length; i++) {
            System.out.println(data[i]);
        }
        boolean flag = shoppingService.deleteArr(data);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

}